package cn.zh.npro.Activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.youth.banner.Banner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.npro.Adapter.GAdapter;
import cn.zh.npro.Adapter.GsAdapter;
import cn.zh.npro.Item.GItem;
import cn.zh.npro.Item.GmItem;
import cn.zh.npro.Item.SgItem;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import cn.zh.npro.Util.GlideImageLoader;
import cn.zh.npro.Util.MyListView;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class GoodDtail extends BaseActivity {

    ImageView gd_back,gtmag;
    GItem gItem;
    MyListView mslist,mlist;
    GsAdapter gsAdapter=null;
    GAdapter gAdapter=null;
    ArrayList<SgItem> sgItems=new ArrayList<SgItem>();
    ArrayList<GmItem> gmItems=new ArrayList<GmItem>();

    Banner gdbanner;
    TextView gname,gcode,gunit,gprice;
    ArrayList<String> imgs=new ArrayList<String>();
    ImageView gdimage;
    View lin1,lin2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_dtail);
        gItem= (GItem) getIntent().getSerializableExtra("GItem");
        gdinti();
        getGdtail(gItem.getGoodsId());
       }

    public void gdinti(){
        gd_back=findViewById(R.id.gd_back);
        gtmag=findViewById(R.id.gtmag);
        mslist=findViewById(R.id.mslist);
        mlist=findViewById(R.id.mlist);
        gdimage=findViewById(R.id.gdimage);
        gdbanner=findViewById(R.id.gdbanner);
        gname=findViewById(R.id.gdname);
        gcode=findViewById(R.id.gcode);
        gunit=findViewById(R.id.gunit);
        gprice=findViewById(R.id.gprice);
        lin1=findViewById(R.id.lin1);
        lin2=findViewById(R.id.lin2);
        gd_back.setOnClickListener(this);
        gtmag.setOnClickListener(this);
        gsAdapter=new GsAdapter(GoodDtail.this,sgItems);
        gAdapter=new GAdapter(GoodDtail.this,gmItems);
        mslist.setAdapter(gsAdapter);
        mlist.setAdapter(gAdapter);
        }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.gd_back:
                finish();
                break;
            case R.id.gtmag:
                Intent intent=new Intent(GoodDtail.this,Gooddw.class);
                intent.putExtra("gid",gItem.getGoodsId());
                intent.putExtra("name",gItem.getGoodsName());
                startActivity(intent);
                break;
            }
          }


      public void getGdtail(String gid){
          OkHttpUtils.get(BaseUrl.getGoodsDetail)
                  .params("goodsId",gid)
                  .execute(new StringCallback() {
                      @Override
                      public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                          System.out.println("--------->商品详情==="+s);
                          try {
                              JSONObject js=new JSONObject(s);
                              boolean State=js.getBoolean("State");
                               if (State){
                                   JSONObject Res=js.getJSONObject("Res");
                                   JSONArray GoodsPriceList=Res.getJSONArray("GoodsPriceList");
                                   JSONArray GoodsStockList=Res.getJSONArray("GoodsStockList");
                                   JSONObject Goods=Res.getJSONObject("Goods");
                                   String GoodsName=Goods.getString("GoodsName");
                                   String GoodsNo=Goods.getString("GoodsNO");
                                   String Unit=Goods.getString("Unit");
                                   String Price_Detail=Goods.getString("Price_Detail");

                                   gname.setText(GoodsName);
                                   gcode.setText("编码:"+GoodsNo);
                                   gunit.setText("单位:"+Unit);
                                   gprice.setText("¥"+Price_Detail);
                                   sgItems.clear();
                                   if (GoodsStockList.length()==0){
                                       lin1.setVisibility(View.GONE);
                                       lin2.setVisibility(View.GONE);
                                   }else {
                                       lin1.setVisibility(View.VISIBLE);
                                       lin2.setVisibility(View.VISIBLE);
                                   for (int j=0;j<GoodsStockList.length();j++){
                                     JSONObject ja=GoodsStockList.getJSONObject(j);
                                      String Unt=ja.getString("Unit");
                                      int Stock=ja.getInt("Stock");
                                      String SndCount=ja.getString("SndCount");
                                      String OrderCount=ja.getString("OrderCount");
                                      String WarehouseName=ja.getString("WarehouseName");
                                      String WarehouseID=ja.getString("WarehouseID");
                                      SgItem sgItem=new SgItem(Unt,Stock,SndCount,OrderCount,WarehouseName,WarehouseID);
                                      sgItems.add(sgItem);
                                       }
                                     gsAdapter.notifyDataSetChanged();
                                      }
                                   gmItems.clear();
                                   for (int i=0;i<GoodsPriceList.length();i++){
                                     JSONObject ji=GoodsPriceList.getJSONObject(i);
                                       String lv=ji.getString("lv");
                                       String baseline=ji.getString("baseline");
                                       String Price=ji.getString("Price");
                                       GmItem gmItem=new GmItem(lv,baseline,Price);
                                       gmItems.add(gmItem);
                                     }

                                   gAdapter.notifyDataSetChanged();

                                   JSONArray GoodsPicList=Res.getJSONArray("GoodsPicList");
                                   for (int i=0;i<GoodsPicList.length();i++){
                                       //此处添加图片数据
                                       String url=GoodsPicList.getString(i);
                                       imgs.add(url);
                                      }
                                      if (GoodsPicList.length()==0){
                                          gdimage.setVisibility(View.VISIBLE);
                                          gdbanner.setVisibility(View.GONE);
                                      }else {
                                          gdimage.setVisibility(View.GONE);
                                          gdbanner.setVisibility(View.VISIBLE);
                                          gdbanner.setImages(imgs).setImageLoader(new GlideImageLoader()).setDelayTime(3000).start();
                                        }

                               }
                          } catch (JSONException e) {
                              e.printStackTrace();
                            }
                        }
                      @Override
                      public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                          super.onError(isFromCache, call, response, e);
                        }
                 });
              }



        }
